<template>
  <div class="vue-box sbot sbot-fluid">
    <!-- 参数栏 -->
    <div class="c-panel">
      <el-form :inline="true">
        <!-- 表单 -->
        <sa-item v-model="m.name" type="text" name="资源名" br />
        <sa-item v-model="m.segmentName" type="text" name="简称" br />
        <sa-item v-model="m.code" type="text" name="识别码" br />
        <sa-item v-model="m.category" type="enum" jtype="4" :jv="dims.RESOURCE_CATEGORY" name="资源类别" br />
        <sa-item name="资源类型" br>
          <template>
            <el-select v-model="m.type" placeholder="资源类型" clearable>
              <el-option v-for="(k, v) in options" :key="v" :label="k" :value="v">
              </el-option>
            </el-select>
          </template>
        </sa-item>
        <sa-item name="所属标签" br>
          <template>
            <el-select v-model="selectTags" placeholder="所属标签" clearable multiple>
              <el-option v-for="(k, v) in dims.TAGS" :key="v" :label="k" :value="v">
              </el-option>
            </el-select>
          </template>
        </sa-item>
        <sa-item v-model="m.url" type="text" name="资源地址" br />
        <sa-item v-model="m.img" type="img-list" name="资源图标" br />
        <sa-item v-model="m.thumbnail" type="img-list" name="缩略图" br />
        <sa-item v-model="m.auditStatus" type="enum" jtype="3" :jv="dims.AUDIT_STATUS" name="审核状态" br />
        <sa-item v-model="m.status" type="enum" jtype="3" :jv="dims.YES_OR_NO" name="启用状态" br />
        <sa-item v-model="m.note" type="textarea" name="描述" br />
        <sa-item v-model="m.sort" type="num" name="序号" br />
        <sa-item name="" br class="s-ok">
          <el-button type="primary" icon="el-icon-plus" @click="ok()">保存</el-button>
        </sa-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import optionsFunc from '@/mixins/resource-options';
export default {
  name: 'rsr-add',
  props: {
    param: Object
  },
  data() {
    return {
      // 超级对象
      m: this.createModel(),
      dims: sa.$sys.getCurd(),
      options: null,
      selectTags: []
    }
  },
  mixins: [optionsFunc],
  watch: {
    'm.category': {
      handler(n, o) {
        if (n && n !== o) {
          this.m.type = null
          this.getOptions(n)
        }
      },
      immediate: true
    }
  },
  methods: {
    createModel() {
      return {}
    },
    // 变更
    ok: function () {
      // 表单校验
      let m = this.m;
      sa.checkNull(m.name, '请输入资源名');
      sa.checkNull(m.code, '请输入识别码');
      sa.checkNull(m.status, '请确认是否启用');
      sa.checkNull(m.type, '请确认类型');

      // 将标准转换未字符串
      if (this.selectTags) {
        m.tags = JSON.stringify(this.selectTags)
      }

      // 添加
      sa.ajax('/rsr/add', m, function (res) {
        if (res.code == 200) {
          sa.alert(res.msg, function () {
            this.m = this.createModel();
            sa.closeModel();
            sa.currView().f5();
          }.bind(this));
        }
      }.bind(this), {
        type: 'post'
      });
    },
  }
}
</script>

<style scoped></style>
